/**
       request.js
     * 封装一个Promise风格的通用请求
     * url - 请求地址
     * option - 包含请求方式、请求参数的配置对象
 */
var app = getApp(); //引入全局app.js，我们可以在globalData中定义一些公用的数据，比如baseUrl、token
const request = (url, options) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${app.globalData.javaApi}${url}`,
      method: options.method,
      data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
      header: {
        'Content-Type': 'application/json; charset=UTF-8',
        "Authorization": "Bearer " + wx.getStorageSync("token"),
        "AppId":app.globalData.appId
      },
      success(response) {
        //if (response.data.code === 200) {
        if (response.data.code === 401) {
          wx.removeStorageSync('token');
          app.globalData.isLogin = false
          app.globalData.bindphone = undefined;
          wx.removeStorageSync('bindphone')
          wx.reLaunch({
            url: '/pages/wode/index',
          })
        }
        resolve(response.data)
        // } else {
        //   wx.showToast({
        //     icon:'error',
        //     title: response.msg,
        //   })
        //     reject(response.data)
        // }
      },
      fail(error) {
        wx.showToast({
          icon: 'error',
          title: '接口无响应',
        })
        reject('接口无响应')
      }
    })
  })
}
const get = (url, options = {}) => {
  return request(url, { method: 'GET', data: options })
}

const post = (url, options) => {
  return request(url, { method: 'POST', data: options })
}

const put = (url, options) => {
  return request(url, { method: 'PUT', data: options })
}

// 不能声明DELETE（关键字）
const remove = (url, options) => {
  return request(url, { method: 'DELETE', data: options })
}
module.exports = {
  //封装get方法
  get, post, put, remove
}